'''
Created on Dec 11, 2010

@author: KafKef
'''


from dexen.system.common.utils import NameMgr
from dexen.system.common.excepts import ServerAlreadyRegisteredException

class RegistryCore(object):
    def __init__(self):
        self.masters = {}
        self.slaves = {}
        self.nodes = {}
        self.new_nodes = set()
        self.name_mgr = NameMgr("NODE")
        self.server_addr = None
        
    def register_server(self, addr):
        if self.server_addr:
            raise ServerAlreadyRegisteredException()
        self.server_addr = addr
    
    def register_node(self, addr):
        name = self.name_mgr.get_name()
        self.new_nodes.add(name)
        self.nodes[name] = addr
        return name

    def get_new_nodes(self):
        new_nodes = {}
        for name in self.new_nodes:
            new_nodes[name] = self.nodes[name]
        self.new_nodes = set()
        return new_nodes
    
    def register_master(self, name, addr):
        self.masters[name] = addr
    
    def register_slave(self, name, addr):
        self.slaves[name] = addr

    def get_master_addr(self, name):
        if self.masters.has_key(name):
            return self.masters[name]
        return None

    def get_server_addr(self):
        return self.server_addr
    
    def get_slave_addr(self, name):
        if self.slaves.has_key(name):
            return self.slaves[name]
        return None
    
    